package com.stu.service.impl;

import com.stu.bean.User;
import com.stu.bean.UserExample;
import com.stu.mapper.UserMapper;
import com.stu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public User loginUser(User user){
        return userMapper.selectByAll(user);
    }

    public int insertUser(User user) {
        UserExample ue = new UserExample();
        ue.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> list = userMapper.selectByExample(ue);
        if(list.size()!=0){
            return 0;
        }else{
            userMapper.insert(user);
            return 1;
        }
    }

    public String selectVip(String username) {
        String i = userMapper.selectVip(username);
        if(i!=null){
            return i;
        }else{
            return null;
        }
    }
    public List<User> getUsers(User user, Date strDate, Date endDate) {
        return userMapper.selectUsers(user,strDate,endDate);
    }


    public int deleteUser(int userId) {
        return userMapper.deleteByPrimaryKey(userId);
    }



    public User selectUserById(int userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public int selectCount() {
        UserExample userExample=new UserExample();
        List<User> users = userMapper.selectByExample(userExample);
        return users.size();
    }

    public User selectH(User user) {
        User user1 = null;
        int u = userMapper.insertSelective(user);
        if(u == 1){
            user1 = userMapper.selectUserByname(user.getHeading());
        }
        return user1;
    }
}
